package com.katian;

import java.io.File;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.prefs.Preferences;

import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.input.DragEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Pane;
import javafx.stage.DirectoryChooser;
import javafx.stage.FileChooser;


public class MergerUtilController {

    @FXML
    private ResourceBundle resources;

    @FXML
    private URL location;

    @FXML
    private AnchorPane root;

    @FXML
    private Pane field1;

    @FXML
    private Pane field2;

    @FXML
    private Pane field3;

    @FXML
    private Pane field4;

    @FXML
    private Pane field5;

    @FXML
    private Button go;

    @FXML
    void initialize() {
        assert root != null : "fx:id=\"root\" was not injected: check your FXML file 'MergerPanel.fxml'.";
        assert field1 != null : "fx:id=\"field1\" was not injected: check your FXML file 'MergerPanel.fxml'.";
        assert field2 != null : "fx:id=\"field2\" was not injected: check your FXML file 'MergerPanel.fxml'.";
        assert field3 != null : "fx:id=\"field3\" was not injected: check your FXML file 'MergerPanel.fxml'.";
        assert field4 != null : "fx:id=\"field4\" was not injected: check your FXML file 'MergerPanel.fxml'.";
        assert field5 != null : "fx:id=\"field5\" was not injected: check your FXML file 'MergerPanel.fxml'.";
        assert go != null : "fx:id=\"go\" was not injected: check your FXML file 'MergerPanel.fxml'.";


        field1.setOpacity(0.0);
        field2.setOpacity(0.0);
        field3.setOpacity(0.0);
        field4.setOpacity(0.0);
        field5.setOpacity(0.0);

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(400);
                    new FadeInLeftBigTransition(field1).play();
                    Thread.sleep(150);
                    new FadeInLeftBigTransition(field2).play();
                    Thread.sleep(150);
                    new FadeInLeftBigTransition(field3).play();
                    Thread.sleep(150);
                    new FadeInLeftBigTransition(field4).play();
                    Thread.sleep(150);
                    new FadeInLeftBigTransition(field5).play();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    @FXML
    void ventesDrop(DragEvent event) {
        System.out.println("Drop: " + event);
    }


    @FXML
    void ventesSelection(MouseEvent event) {
        String initialDirectory = Preferences.userRoot().get("VentesInitialDirectory", null);
        FileChooser fileChooser = new FileChooser();
        if (initialDirectory != null) {
            File file = new File(initialDirectory);
            if (file.isDirectory()) {
                fileChooser.setInitialDirectory(file);
            }
        }

        final File file = fileChooser.showOpenDialog(root.getScene().getWindow());
        if (file != null) {
            Preferences.userRoot().put("VentesInitialDirectory", file.getParentFile().getAbsolutePath());
        }
    }


    @FXML
    void destinationSelection(MouseEvent event) {
        String initialDirectory = Preferences.userRoot().get("DestinationInitialDirectory", null);
        DirectoryChooser fileChooser = new DirectoryChooser();
        if (initialDirectory != null) {
            File file = new File(initialDirectory);
            if (file.isDirectory()) {
                fileChooser.setInitialDirectory(file);
            }
        }

        final File file = fileChooser.showDialog(root.getScene().getWindow());
        if (file != null) {
            Preferences.userRoot().put("DestinationInitialDirectory", file.getAbsolutePath());
        }
    }


}
